<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Warehouse extends Backend_Controller {
	
	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct ();
		$this->isValidRequest ();
		$this->load->model ( 'backend/Warehouse_model', 'Warehouse' );
		// 加载验证类
		$this->load->library ( 'form_validation' );
	}
	/**
	 * 显示供货商库存
	 */
	public function index() {
		$result = $this->Warehouse->getWarehouse($this->page, $this->input->post());
		$this->load->view ( 'backend/warehouse/index', $result);
	}
	/**
	 * 添加供货商库存
	 */
	public function add() {
		if ($this->isPost ()) {
			$this->form_validation->validateData(array(
				'district_districtName',
				'district_id',
			), array(
				array('district_districtName', 'required'),
				array('district_id', 'required|integer')
			));
			if($this->form_validation->hasErrors()) {
				$this->renderJson(300, $this->form_validation->error);
			}
			
			$product = $this->post('products');		
			if (empty ( $product )) {
				$this->renderJson ( 300 );
			}
			$data = array ();
			foreach ($product as $v) {
				if(intval($v['items_orgNum']) < 1 || intval($v['number']) < 1) {
					$this->renderJson(300);
				}
				$data[] = array(
					'address_id' => $this->form_validation->district_id,
					'product_id' => intval($v['items_orgNum']),
					'request_num' => intval($v['number'])
				);
			}
			$result = $this->Warehouse->addStock($data);
			if($result) {
				$this->renderJson ( 200, '', array (
						'navTabId' => 'backendwarehouseindex',
						'callbackType' => 'closeCurrent' 
				) );
			}
			$this->renderJson(300);
		}
		
		$this->load->view ( 'backend/warehouse/add' );
	}
	public function addAgent() {
		$this->load->model('backend/Areas_model', 'Areas');
		$result = $this->Areas->getAreasAll($this->page, $this->input->post(), array('ae.type'=>'2'));
		$result ['list'] = $this->Areas->searchAreas(0);
		$this->load->view ( 'backend/warehouse/addAgent', $result );
	}
	public function addProduct() {
		$this->load->model ( 'backend/Product_model', 'Product' );
		$result = $this->Product->getProductAll ( $this->page, $this->input->post () );
		$this->load->model('backend/Category_model', 'Category');
		$result['categoryList'] = $this->Category->categoryList();
		$this->load->view ( 'backend/warehouse/addProduct', $result );
	}
}